import { doLogin } from '@/api/login'
import { Dialog, Toast } from 'vant'
import router from './../../router'

export default {
  namespaced: true,
  state: {
    // 获取信息
    isLogn: localStorage.getItem('isLogin') === 'true',
    isShowFooter: localStorage.getItem('isShowFooter') === 'true'
  },
  mutations: {
    // 改变数据
    changeIsLogin (state, payload) {
      state.isLogin = payload
    },
    // 底部是否显示
    changeIsShowFooter (state, payload) {
      state.isShowFooter = payload
    }
  },
  actions: {
    loginAction (context, params) {
      doLogin(params).then(res => {
        if (res.data.code === '10010') {
          Dialog.confirm({
            message: '该用户还未注册',
            confirmButtonText: '立即注册',
            cancelButtonText: '取消'
          }).then(() => {
            this.$router.push('/register')
          }).catch(() => {})
        } else if (res.data.code === '10011') {
          Toast('密码错误')
        } else {
          localStorage.setItem('token', res.data.data.token)
          localStorage.setItem('userid', res.data.data.userid)
          localStorage.setItem('isLogin', true)
          localStorage.setItem('isShowFooter', true)
          context.commit('changeIsLogin', true)
          router.back()
        }
      })
    },
    footerAction (context, params) {
      context.commit('changeIsShowFooter', params)
    },
    logoutAction () {}
  }
}
